package com.quicklife.service.impl;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import com.quicklife.pojo.Business;
import com.quicklife.pojo.Tuser;
import com.quicklife.service.inter.FileUploadService;
import com.quicklife.service.inter.RegisterService;
import com.quicklife.util.JsonUtil;
import com.quicklife.util.SqlHelper;
import com.quicklife.util.StringUtil;

/**
 * 注册逻辑处理
 * 
 * @author HackerD
 * 
 */
public class RegisterServiceImpl implements RegisterService {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.quicklife.service.inter.RegisterService#webRegister(java.lang.String)
	 */
	@Override
	public String webRegister(String rqinfos) {
		try {
			Business buss = (Business) JsonUtil.toBean(rqinfos, Business.class);
			String result = null;

			// 判断登陆用户名是否已被注册
			result = SqlHelper.checkBeExist("business where LOGIN_NAME=?",
					new String[] { buss.getLogin_name() },
					StringUtil.FAIL_LNAME_EXIST);
			if (result != null) {
				return result;
			}

			// 判断手机号是否已被注册
			result = SqlHelper.checkBeExist("business where PHONE=?",
					new String[] { buss.getPhone() },
					StringUtil.FAIL_PHONE_EXIST);
			if (result != null) {
				return result;
			}

			int tableid = SqlHelper.getTableId("business");
			buss.setId(tableid);

			String rtinfos = commRegister(
					"insert into business(id,type,login_name,password,name,"
							+ "phone,address,logo,lat,lon) values"
							+ "(?,?,?,?,?,?,?,?,?,?)",
					new String[] { StringUtil.toString(tableid),
							StringUtil.toString(buss.getType()),
							buss.getLogin_name(), buss.getPassword(),
							buss.getName(), buss.getPhone(), buss.getAddress(),
							buss.getLogo(), StringUtil.toString(buss.getLat()),
							StringUtil.toString(buss.getLon()) }, buss);

			return rtinfos;
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
					StringUtil.RESULT_EXCEPTION);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.quicklife.service.inter.RegisterService#androidRegister(java.lang
	 * .String)
	 */
	@Override
	public String androidRegister(HttpServletRequest request) {
		try {
			FileUploadService fileUploadService = new FileUploadServiceImpl();
			String tempInfos = fileUploadService.getUploadInfos(request);
			JSONObject jsonObject = JSONObject.fromObject(tempInfos);
			Tuser user = (Tuser) JsonUtil.toBean(
					jsonObject.getString("rqinfos"), Tuser.class);
			if (jsonObject.containsKey("photo")) {
				user.setPhoto(jsonObject.getString("photo"));
			}

			String result = null;

			// 判断登陆用户名是否已被注册
			result = SqlHelper.checkBeExist("Tuser where LOGIN_NAME=?",
					new String[] { user.getLogin_name() },
					StringUtil.FAIL_LNAME_EXIST);
			if (result != null) {
				return result;
			}

			// 判断昵称是否已被注册
			result = SqlHelper.checkBeExist("Tuser where nickname=?",
					new String[] { user.getNickname() },
					StringUtil.FAIL_NICKNAME_EXIST);
			if (result != null) {
				return result;
			}

			// 判断手机号是否已被注册
			result = SqlHelper.checkBeExist("Tuser where BIND_PHONE=?",
					new String[] { user.getBind_phone() },
					StringUtil.FAIL_PHONE_EXIST);
			if (result != null) {
				return result;
			}

			// 判断邮箱是否已被注册
			// result = checkBeExist("Tuser where Email=?",
			// new String[] { user.getEmail() },
			// StringUtil.FAIL_EMAIL_EXIST);
			// if (result != null) {
			// return result;
			// }

			int tableid = SqlHelper.getTableId("business");
			user.setId(tableid);

			String rtinfos = commRegister(
					"insert into tuser(ID,LOGIN_NAME,NICKNAME,PASSWORD,SEX,"
							+ "BIND_PHONE,LAST_LOGIN_TIME,PHOTO,EMAIL)"
							+ "values(?,?,?,?,?,?,sysdate,?,?)",
					new String[] { StringUtil.toString(tableid),
							user.getLogin_name(), user.getNickname(),
							user.getPassword(),
							StringUtil.toString(user.getSex()),
							user.getBind_phone(), user.getPhoto(),
							user.getEmail() }, user);

			// 保存文件
			fileUploadService.saveFile();

			return rtinfos;
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
					StringUtil.RESULT_EXCEPTION);
		}
	}

	/**
	 * 将注册信息插入数据库
	 * 
	 * @param sql
	 * @param params
	 * @param tableid
	 * @return
	 */
	private String commRegister(String sql, String[] params, Object object) {
		try {
			SqlHelper.executeUpdate(sql, params);
		} catch (Exception e) {
			e.printStackTrace();
			return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_FAIL,
					StringUtil.RESULT_EXCEPTION);
		}

		return JsonUtil.createRtJsonStr(StringUtil.RESULT_CODE_OK, object);
	}
}
